package com.chenjiajin.fastjson_test.nullData;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.chenjiajin.domain.TestPoJo;
import com.chenjiajin.domain.TestPoJo2;

public class NullData {

    // 演示: fastjson 的 JSON.toJSONString() 默认忽略值为 null 的属性
    public static void main(String[] args) {
        TestPoJo2 outside = new TestPoJo2();
        TestPoJo inside = new TestPoJo("as1111das", "fhgfgh");
        outside.setName(inside);

        String jsonStr = JSON.toJSONString(outside);
        System.err.println("jsonStr = " + jsonStr);
        /**    可以看到只有值不是null的属性可以序列化 , 其他的属性默认忽略
         * {
         *     "name": {
         *         "name": "as1111das",
         *         "path": "fhgfgh"
         *     }
         * }
         */

        // 解决方法:
        String jsonStr2 = JSON.toJSONString(outside, SerializerFeature.WriteMapNullValue);
        System.err.println("jsonStr2 = " + jsonStr2);
        /**
         * {
         *     "args": null,
         *     "hobby": null,
         *     "name": {
         *         "args": null,
         *         "hobby": null,
         *         "id": null,
         *         "name": "as1111das",
         *         "path": "fhgfgh"
         *     },
         *     "path": null
         * }
         */
    }

}